package com.test.ch07;

import java.util.*;

class Student implements Comparable<Student> {
    String id;
    String name;
    int age;
    double score;

    public Student(String id, String name, int age, double score) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public int compareTo(Student other) {
        // 1. 成绩逆序 (从高到低)
        int scoreCompare = Double.compare(other.score, this.score);
        if (scoreCompare != 0) return scoreCompare;

        // 2. 姓名降序 (按字母倒序)
        int nameCompare = other.name.compareTo(this.name);
        if (nameCompare != 0) return nameCompare;

        // 3. 年龄升序 (从小到大)
        int ageCompare = Integer.compare(this.age, other.age);
        if (ageCompare != 0) return ageCompare;

        // 4. ID升序 (从小到大)
        return this.id.compareTo(other.id);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Double.compare(student.score, this.score) == 0 &&
                Objects.equals(id, student.id) &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, score);
    }

    @Override
    public String toString() {
        return String.format("学号:%s, 姓名:%s, 年龄:%d, 分数:%.1f", id, name, age, score);
    }
}

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // C) 创建TreeMap集合，Student已实现Comparable接口，会自动排序
        Map<Student, String> studentMap = new TreeMap<>();

        System.out.println("请输入学生信息（格式：学号$姓名$年龄$分数），输入'quit'结束：");

        // A) 键盘录入，遇到quit结束
        while (true) {
            String input = sc.nextLine().trim();
            if ("quit".equalsIgnoreCase(input)) {
                break;
            }

            // 跳过空行
            if (input.isEmpty()) {
                continue;
            }

            // B) 拆解字符串，实例化学生对象
            String[] parts = input.split("\\$");
            if (parts.length == 4) {
                try {
                    String id = parts[0];
                    String name = parts[1];
                    int age = Integer.parseInt(parts[2]);
                    double score = Double.parseDouble(parts[3]);

                    // 创建学生对象并添加到Map
                    Student student = new Student(id, name, age, score);
                    studentMap.put(student, student.id);
                    System.out.println("成功添加学生: " + student);

                } catch (NumberFormatException e) {
                    System.out.println("数字格式错误，请检查年龄和分数是否为有效数字: " + input);
                }
            } else {
                System.out.println("输入格式错误，请按照'学号$姓名$年龄$分数'格式输入: " + input);
            }
        }

        // D) 遍历集合 (方式1: EntrySet)
        System.out.println("\n=== 所有学生信息 (EntrySet遍历) ===");
        System.out.println("学生总数: " + studentMap.size());
        for (Map.Entry<Student, String> entry : studentMap.entrySet()) {
            System.out.println(entry.getKey());
        }

        // E) 判断003号学生是否存在并删除
        System.out.println("\n=== 删除操作 ===");
        Student studentToRemove = null;
        for (Student student : studentMap.keySet()) {
            if ("003".equals(student.id)) {
                studentToRemove = student;
                break;
            }
        }

        if (studentToRemove != null) {
            studentMap.remove(studentToRemove);
            System.out.println("成功删除003号学生: " + studentToRemove);
        } else {
            System.out.println("003号学生不存在。");
        }

        // F) 再次遍历集合 (方式2: KeySet)
        System.out.println("\n=== 最终学生列表 (KeySet遍历) ===");
        System.out.println("剩余学生数: " + studentMap.size());
        for (Student student : studentMap.keySet()) {
            System.out.println(student);
        }

        sc.close();
    }
}